package com.imooc.ecommerce.config;

import com.imooc.ecommerce.filter.LoginUserInfoInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * <h1>Web Mvc 配置</h1>
 *
 * @author : huangguangyue
 * @date : 2024/12/9 12:01
 */
@Configuration
public class ImoocWebMvcConfig extends WebMvcConfigurationSupport {

    /**
     * <h2>添加拦截器配置</h2>
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        // 添加用户省份统一登录拦截的拦截器
        registry.addInterceptor(new LoginUserInfoInterceptor())
                // .addPathPatterns("/**")：这行代码指定了拦截器的路径模式。/** 表示匹配所有的请求路径
                .addPathPatterns("/**")
                // 设置了拦截器的执行顺序。order(0) 表示该拦截器的执行顺序最低，将在其他拦截器之前执行
                .order(0);
    }

    /**
     * <h2>让 MVC 假装 Swagger 的静态资源</h2>
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").
                addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}
